import { defineStore } from 'pinia'

export const useUserStore = defineStore({
    id: 'user',
    state: () => {
        return {
            token: '',
            isAdmin: false,
            IsLogin: false,
            show: false,
            title: '',
            userName: '',
        }
    },
    actions: {
        showPop(k: boolean, bool: boolean) {
            this.IsLogin = k
            this.show = bool
        },
        showCreate(bool: boolean) {
            this.isAdmin = bool
        },
        setToken(key: string) {
            this.token = key
        },
        setUsername(key: string) {
            this.userName = key
        }
    },
    getters: {
        getTitle(state) {
            if (state.IsLogin) {
                return '登录'
            } else {
                return '注册'
            }
        }
    },
    persist: {
        //这里存储默认使用的是session
        enabled: true,
        strategies: [
            {
                //key的名称
                key: 'my_user',
                //更改默认存储，我更改为localStorage
                storage: localStorage,
                // 可以选择哪些进入local存储，这样就不用全部都进去存储了
                // 默认是全部进去存储
                paths: ['token', 'isAdmin', 'userName']
            }
        ]
    }

})